﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using PepsiCo.Business;
using System.Web.Script.Serialization;
using System.Collections;

namespace PepsiCo.UI 
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        PepsiCoDBDataContext db = new Business.PepsiCoDBDataContext(LocalSetting.ConnectionString);
 
        #region Role
        [WebMethod]
        public object RoleGetById(int roleId)
        {
            return db.pepsi_Roles
                .Select(p => new
                {
                    p.RoleId,
                    p.RoleName,
                    p.Description,
                })
                .FirstOrDefault(p => p.RoleId == roleId);
        }

        #endregion

        #region User
        [WebMethod]
        public bool CheckExistUser(string username)
        {
            if (db.pepsi_Users.Any(p => p.Name == username)) return true;
            return false;
        }
        #endregion
    
        #region MO-MT
        [WebMethod]
        public object GetMT(Guid mo_guid)
        {
            return db.SmsMtStatistics.Where(p => p.MoGuid == mo_guid)
                        .Select(p => new
                        {
                            p.MtGuid,
                            p.MoGuid,
                            p.ShortCode,
                            p.DestAddr,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.SentTime,
                            p.CDRIndicator,
                            p.MTType
                        });
        }

        [WebMethod]
        public object GetMO(Guid mo_guid)
        {
            return db.SmsMoStatistics.Where(p => p.MoGuid == mo_guid)
                        .Select(p => new
                        {
                            p.MoGuid,
                            p.ShortCode,
                            PhoneNumber = p.Subs,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.ReceivedTime
                        });
        }

        [WebMethod]
        public object GetMO_MT(Guid mo_guid)
        {
            var mt= db.SmsMtStatistics.Where(p => p.MoGuid == mo_guid)
                        .Select(p => new
                        {
                            p.MtGuid,
                            p.MoGuid,
                            p.ShortCode,
                            PhoneNumber = p.DestAddr,
                            p.OperatorID,
                            p.Message,
                            p.CooperateID,
                            p.Status,
                            p.SentTime,
                            p.CDRIndicator,
                            p.MTType
                        });
            var mo = db.SmsMoStatistics.Where(p => p.MoGuid == mo_guid)
                       .Select(p => new
                       {
                           p.MoGuid,
                           p.ShortCode,
                           PhoneNumber = p.Subs,
                           p.OperatorID,
                           p.Message,
                           p.CooperateID,
                           p.Status,
                           p.ReceivedTime
                       });
            return new ArrayList { mo, mt };
        }
        #endregion

        #region Staff
        [WebMethod]
        public object GetStaffByID(Guid staffId)
        {
            return db.pepsi_Staffs.Select(p => new
            {
                p.StaffID,
                p.StaffName,
                UnitName = p.pepsi_Unit.Name,
                p.pepsi_StaffType.StaffTypeName,
                p.StaffLevel
            })
            .FirstOrDefault(p => p.StaffID == staffId);
        }
        #endregion
    }
}
